<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class User_favorite extends CI_Model {

	const DB_CAIPU = 'caipu';
	const DB_FAVORITE = 'caipu_favorite';

	const DB_CAIDAN = 'caidan';
	const DB_FAVORITE_CAIDAN = 'caidan_favorite';

	function __construct()
	{
		parent::__construct();
	
	}
	//查询用户收藏的菜谱
	function get_caipu_fav_by_user($user_id, $offset = 0, $limit = 6)
	{
		$this->db->select("".self::DB_CAIPU.".id, ".self::DB_CAIPU.".title, ".self::DB_CAIPU.".coverImage, ".self::DB_CAIPU.".costtime, ".self::DB_CAIPU.".authorid, ".self::DB_CAIPU.".author");
		$this->db->join(self::DB_FAVORITE, "".self::DB_FAVORITE.".caipu_id = ".self::DB_CAIPU.".id");

		$this->db->where("uid", $user_id);
		$this->db->limit(intval($limit));
		$this->db->offset(intval($offset));
		return $this->db->get(self::DB_CAIPU);
	}

	//根据菜谱ID 查询 那些有用户收藏
	function get_caipu_fav_user($caipu_id, $limit = 6)
	{
		$this->db->select('*')->from(self::DB_FAVORITE)->where('caipu_id', intval($caipu_id))->limit(intval($limit));
		return $this->db->get();
	}
	//根据用户ID，菜谱ID 查询 0为空
	function get_caipu_favorite($caipu_id, $user_id)
	{
		$array = array(
			'uid' => intval($user_id), 
			'caipu_id' => intval($caipu_id)
		);
		return  $this->db->get_where(self::DB_FAVORITE, $array)->num_rows();
	}
	//插入我收藏的菜谱
	function add_caipu_favorite($caipu_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'caipu_id' => intval($caipu_id)
		);

		return $this->db->insert(self::DB_FAVORITE, $array);
	}
	//删除我收藏的菜谱
	function del_caipu_favorite($caipu_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'caipu_id' => intval($caipu_id)
		);
		return $this->db->delete(self::DB_FAVORITE, $array);
	}
	function del_user_id_caipu($user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id)
		);
		return $this->db->delete(self::DB_FAVORITE, $array);
	}
	//************************菜单部分*****************************************

	//查询用户收藏的菜单
	function get_caidan_fav_by_user($user_id, $offset = 0, $limit = 6)
	{
		$this->db->select("".self::DB_CAIDAN.".id, ".self::DB_CAIDAN.".title, ".self::DB_CAIDAN.".uptime, ".self::DB_CAIDAN.".desc, ".self::DB_CAIDAN.".uid");
		$this->db->join(self::DB_FAVORITE_CAIDAN, "".self::DB_FAVORITE_CAIDAN.".caidan_id = ".self::DB_CAIDAN.".id");
		$this->db->where("".self::DB_FAVORITE_CAIDAN.".uid", $user_id);
		$this->db->limit(intval($limit));
		$this->db->offset(intval($offset));
		return $this->db->get(self::DB_CAIDAN);
	}
	//根据菜单ID 查询 那些有用户收藏
	function get_caidan_fav_user($caidan_id, $limit = 6)
	{
		$this->db->select('*')->from(self::DB_FAVORITE_CAIDAN)->where('caidan_id', intval($caidan_id))->limit(intval($limit));
		return $this->db->get();
	}
	//根据用户ID，菜单ID 查询 0为空 
	function get_caidan_favorite($caidan_id, $user_id)
	{
		$array = array(
			'uid' => intval($user_id), 
			'caidan_id' => intval($caidan_id)
		);
		return  $this->db->get_where(self::DB_FAVORITE_CAIDAN, $array)->num_rows();
	}
	//插入我收藏的菜单
	function add_caidan_favorite($caidan_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'caidan_id' => intval($caidan_id)
		);

		return $this->db->insert(self::DB_FAVORITE_CAIDAN, $array);
	}
	//删除我收藏的菜单
	function del_caidan_favorite($caidan_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'caidan_id' => intval($caidan_id)
		);
		return $this->db->delete(self::DB_FAVORITE_CAIDAN, $array);
	}
	function del_user_id_caidan($user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id)
		);
		return $this->db->delete(self::DB_FAVORITE_CAIDAN, $array);
	}
}

/* End of file user_favorite.php */
/* Location: ./application/models/user/user_favorite.php */